package com.etc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.etc.entity.CareerClassify;
import com.etc.tools.DBTools;

public class CareerClassifyDAO {

	DBTools tool = new DBTools();
	Connection conn = tool.getConnection();
	//添加一个职业
	public void addClassify(CareerClassify classify) {
		String sql = "insert into t_career_classify values(?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, classify.getClassId());
			ps.setString(2, classify.getClassName());
			ps.setString(3, classify.getClassDesc());
			int confrim = ps.executeUpdate();
			if(confrim==1) {
				System.out.println("添加成功");
			}
		} catch (SQLException e) {
			System.out.println("职业分类添加异常");
			e.printStackTrace();
		}
	}
	//展示所有的职业分类
	public ArrayList<CareerClassify> showClassify() {
		ArrayList<CareerClassify> classifys = new ArrayList<>();
		String sql = "select * from t_career_classify";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ResultSet set = ps.executeQuery();
			while(set.next()) {
				CareerClassify classify = new CareerClassify(
						set.getString("class_id"),
						set.getString("class_name" ),
						set.getString("class_desc"));
				classifys.add(classify);
			}
			
		} catch (SQLException e) {
			System.out.println("职业分类查询异常");
			e.printStackTrace();
		}
		return classifys;
	}
	//根据id获取职业分类对象
	public CareerClassify findClassById(String classId) {
		String sql = "select * from T_CAREER_CLASSIFY where CLASS_ID = ?";
		PreparedStatement ps = null;
		CareerClassify classify = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, classId);
			ResultSet set = ps.executeQuery();
			set.next();
			classify = new CareerClassify(set.getString("class_id"), 
					set.getString("class_name"), set.getString("class_desc"));
		} catch (SQLException e) {
			System.out.println("根据id获取职业分类对象查询异常");
			e.printStackTrace();
		}
		
		return classify;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
